package com.coldtech.circlecrash;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
	public final static SpriteSheet IMAGEBUTTON = new SpriteSheet(
			"/imagebutton_background.png", 48, 48);
	
	public final static SpriteSheet TEXTBUTTON = new SpriteSheet(
			"/textbutton_background.png", 9, 9);

	public final static SpriteSheet IMG = new SpriteSheet("/image.png", 48, 48);

	private Sprite[] sprite = null;

	private int rows, columns;
	private int width, height;

	public SpriteSheet(String path, int width, int height) {
		this.width = width;
		this.height = height;

		BufferedImage image = null;
		try {
			image = ImageIO.read(SpriteSheet.class.getResource(path));
		} catch (IOException e) {
			e.printStackTrace();
		}

		this.rows = image.getWidth() / this.width;
		this.columns = image.getHeight() / this.height;
		sprite = new Sprite[rows * columns];

		for (int r = 0; r < this.rows; r++) {
			for (int c = 0; c < this.columns; c++) {
				sprite[r * this.columns + c] = new Sprite(
						image.getSubimage(r * this.width, c * this.height,
								this.width, this.height), this.width,
						this.height);
			}
		}
	}

	public Sprite getSprite(int row, int column) {
		if ((row >= 0 && row < rows) && (column >= 0 && column < columns)) {
			return sprite[row * columns + column];
		}
		return null;
	}
}
